package Greedy;

public class _861_ScoreAfterFlippingMatrix {
    //hint: two steps:1.transfer all value in column[0] to 1;2.from high to low position,transfer every
    //column's value to it's max(get the most 1 values)
    public int matrixScore(int[][] A) {
        int row = A.length;
        int column = A[0].length;
        int ans = 0;
        for (int c = 0; c < column; c++) {
            int count = 0;
            for (int r = 0; r < row; r++) {
                //elegant code:when in column 0,doesn't need to change any value,
                //then in the rest column,the values in column 0 are all 1,rest value exclusive or
                //to 1 to get 0's count,then judge if this column need to be transfered
                count += A[r][c] ^ A[r][0];
            }
            ans += Math.max(count, row - count) * (1 << (column - 1 - c));
        }
        return ans;
    }
}
